#!/user/bin/python
# -*- coding: utf-8 -*-

import urllib2
import re

class Article:
    #初始化
    def __init__(self):
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers = { 'User-Agent' : self.user_agent }
        
    #获取传入网页内容    
    def getContent(self, url):
        request = urllib2.Request(url, headers = self.headers)
        response = urllib2.urlopen(request)
        content = response.read().decode('utf-8')
        return content
    
    def getArticle(self, content, reg):
        pattern = re.compile(reg, re.S)
        items = re.findall(pattern, content)
        return items
    
url = 'http://jinnianshilongnian.iteye.com';   
article = Article()
content = article.getContent(url + '/blog/2018398')
items = article.getArticle(content, '<p.?style="font-size: 14px;">\s*?<a\s?href="(/blog/.*?)"\starget="_.{0,6}">.*?</a></p>')
for item in items:
    content = article.getContent(url + item)
    title = article.getArticle(content, '<div.?class="blog_title">.*?<h3>.*?<a.?href=".*">(.*?)</a>.*?</h3>.*?</div>.*?'+
                               '<div.?id="blog_content".?class="blog_content">.*?<div.*?class="iteye-blog-content-contain">(.*?)</div></div>')
    print(title.encode('gbk'))


